//
//  MCBasicAppEngine.m
//  MCPublicModule
//
//  Created by Mirko on 16/7/4.
//  Copyright © 2016年 Mirko. All rights reserved.
//

#import "MCBasicAppEngine.h"
#import "HDEasterEggsCenter.h"
#import "MBProgressHUD.h"

@interface MCBasicAppEngine ()

#pragma mark alert
@property (nonatomic, strong) MBProgressHUD *alert;

#pragma mark HDEasterEggsCenter
@property (nonatomic, assign) BOOL openEasterEggs;

#pragma mark HDOperationService


@end

@implementation MCBasicAppEngine

+(instancetype) instance
{
    static MCBasicAppEngine *engine = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        engine = [[MCBasicAppEngine alloc] init];
    });
    return engine;
}

-(instancetype) init
{
    self = [super init];
    if (self){
    }
    return self;
}

+(void) startWithEngine:(UIWindow *) window
{
    [MCBasicAppEngine instance].window = window;
}

#pragma mark alert

+(void) showTostAlert:(NSString *) msg
{
    [[MCBasicAppEngine instance] showAleart:msg];
}

-(void) showAleart:(NSString *) msg
{
    if (!self.alert) {
        self.alert = [[MBProgressHUD alloc] initWithWindow:self.window];
    }
    if (!self.alert.superview) {
        [self.window addSubview:self.alert];
    }
    self.alert.blur = NO;
    self.alert.labelText = msg;
    self.alert.mode = MBProgressHUDModeText;
    self.alert.removeFromSuperViewOnHide = YES;
    [self.alert showAnimated:YES whileExecutingBlock:^{
        sleep(1.5);
        [self.alert hide:YES];
    }];
}


@end

@implementation MCBasicAppEngine (VersionCompatibility)

-(void) installVersionCompatibility
{
    self.compatibility = [[MCBasicVersionCompatibility alloc] init];
}

@end

@implementation MCBasicAppEngine (EasterEggs)

-(void) installEasterEggs;
{
    self.openEasterEggs = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [tap addTarget:self action:@selector(handleGesture:)];
    tap.numberOfTouchesRequired = 2;
    tap.numberOfTapsRequired = 1;
    [self.window addGestureRecognizer:tap];
    [HDEasterEggsCenter easterEggsCenter];
}

-(void)handleGesture:(UIGestureRecognizer*)gestureRecognizer
{
    if (!self.openEasterEggs) {
        return;
    }
    [[HDEasterEggsCenter easterEggsCenter] showEasterEggs];
}

-(void) openEasterEggs:(BOOL) open
{
    self.openEasterEggs = open;
}

@end

@implementation MCBasicAppEngine (OperationService)

-(void) installOperationService
{
    
}

@end
